<?php
	/**
	 * UserAction 企业用户控制器
	 * @access public
	 */
class UserAction extends Action
{
	//显示登陆页面
	public function index(){
		$this->display('Index/login');
	}

	//用户登陆
	//登陆成功后设置userid,username两个Session变量
	public function login(){
		if(empty($_POST['accountname'])) {
			$this->error('帐号必须！');
		}elseif (empty($_POST['password'])){
			$this->error('密码必须！');
		}elseif (empty($_POST['verify'])){
			$this->error('验证码必须！');
		}
		if($_SESSION['verify'] != md5($_POST['verify'])) {
			$this->error('验证码错误！');
		}
		$model = M("AirportEnterpriseInformation");
		$list = $model->where("accountname = '{$_POST['accountname']}'")->find();
		if(empty($list)){
			$this->error('账号有误,请重新输入');
		}else if($list['password'] === md5($_POST['password'])){
			Session::set('userid',$list['id']);
			Session::set('username',$list['accountname']);
			$this->assign('jumpUrl','__GROUP__/Index');
			$this->success('登陆成功');
		}else{
			$this->error('密码有误');
		}
	}

	//注销登陆
	public function logout(){
		Session::destroy();
		$this->assign('jumpUrl','__APP__');
		$this->success("注销成功");
	}
	
	// 输出验证码图片
	public function verify(){
		import("ORG.Util.Image");
		Image::buildImageVerify();
	}
	//用户注册
	public function regist(){
		if(!empty($_POST)){
			$model = D("Home.AirportEnterpriseInformation");
			if($model->create()){
				$model->publishtime = time();
				$model->password= md5($_POST['password']);
				$list = $model->add();
				//dump($list);
				if($list !== false ){
					Session::set('username',$_POST['accountname']);
					Session::set('userid',$list);
					$this->assign('jumpUrl',"__APP__");
					$this->success("注册成功,正为你跳转到首页,请稍后..");
				}else{
					$this->error("注册失败!请重试!");
				}
			}else{
				$this->error($model->getError());
			}
		}
		$this->display("Index/regist");
	}

	public function modify(){
		$model = M("AirportEnterpriseInformation");
		$instance=$model->where("id=".Session::get('userid'))->select();
		$this->assign("vo",$instance);
		if($model->create()){
			$data['accountname'] = $_POST['accountname'];
			$data['password'] = md5($_POST['password']);
			$data['accountname'] = $_POST['accountname'];
			$data['enterprise_name'] = $_POST['enterprise_name'];
			$data['enterprise_type'] = $_POST['enterprise_type'];
			$list = $model->where("id={$_POST['id']}")->save($data);
			if($list !== false){
				Session::set('username',$_POST['accountname']);
				Session::set('userid',$list);
				$this->redirect("/Index/index");
			}else{
				$this->error("修改失败!请重试!");
			}
		}
		$this->display("Index/modify");
	}
}
?>
